<?php
//login controller

class loginController extends action{

	public function __Construct() {
		parent::__Construct();
	}
	
public function index(){
	
		if ( $_SESSION['user']!='' ){
			$this->jump('?g=login&c=index');
		}
		//print_r($_SESSION);
		$this->template();
		$this->assign('title', 'Login');
		$_SESSION['formkey'] = md5(time().'naryn');
		$this->assign('formkey',$_SESSION['formkey']);
		$this->display('index');
	}
	
	
	public function logining(){
		//check post
		if($_SESSION['formkey']!= $_POST['formkey']){
			$this->error('Invalid form !','?g=login&c=index&a=login');
		}
		
		if ($_SESSION['VCODE']!=md5($_POST['yzm'])){
			$this->error('VCODE error !','?g=login&c=login');
		}
		$user = trim($_POST['user']);
		$pwd  = md5(trim($_POST['pwd']));
		$_POST = ''; unset($_POST);
		$obj = $this->M('users');
		$result = $obj->getUserByUserName($user);
		//print_r($result);exit;
		if ($result){
			if ($result[0]['user_pwd'] != $pwd){
				$this->error('Login  failed :0 !','?g=login&c=index&a=login');
			}else{
				$_SESSION['user'] = $result[0];
				$this->success('Login success !','?g=admin&c=index');
			}
		}else{
			unset($_SESSION);
			$this->error('Login failed :1 !','?g=login&c=index&a=login');
		}
	}
	
	
	
	public function logout(){
		$_SESSION['user'] = '';
		unset($_SESSION);
		$this->success('Logout success !','?g=login&c=login&a=index');

	}
	
	public function chkLogin(){
		
		if (!$_SESSION['user']){
			$this->jump('?g=login&c=login');
		}
        return ture;
	}
	

}